
package com.csi.servlet;

import com.csi.dao.InformDao;
import com.csi.model.Inform;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author sunran
 * @date 10:24 2019/10/25
 */
@WebServlet("/inform")
public class InformServlet extends HttpServlet {

  private InformDao informDao = new InformDao();

  @Override
  protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String type = req.getParameter("type");
    switch (type) {
      case "queryAll":
        queryAll(req, resp);
        break;
      case "queryByKey":
        queryByKey(req, resp);
        break;
      case "queryByPage":
        queryByPage(req, resp);
        break;
      case "update":
        showInform(req, resp);
        break;
      case "preview":
        preview(req, resp);
        break;
      case "delete":
        delete(req, resp);
        break;
        default:
    }
  }

  private void queryByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    int currentPage = Integer.valueOf(req.getParameter("currentPage"));
    int pageSize = Integer.valueOf(req.getParameter("pageSize"));
    String dir = req.getParameter("dir");


    List<Inform> informs = informDao.queryByPage(currentPage, pageSize);

    req.setAttribute("informList", informs);
    req.setAttribute("currentPage", currentPage);
    req.getRequestDispatcher("/notice/notice.jsp").forward(req, resp);
  }

  private void queryByKey(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String title = req.getParameter("title");
    String content = req.getHeader("content");
    Map<String, String> map = new HashMap<>();
    map.put("title", title);
    map.put("content", content);

    List<Inform> informs = informDao.queryByKey(map);
    req.setAttribute("informList", informs);
    req.setAttribute("currentPage", 1);
    req.getRequestDispatcher("/notice/notice.jsp").forward(req, resp);
  }

  private void preview(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String id = req.getParameter("id");
    Inform inform = informDao.queryById(Integer.valueOf(id));

    req.setAttribute("inform", inform);

    req.getRequestDispatcher("/notice/previewNotice.jsp").forward(req, resp);
  }

  @Override
  protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String type = req.getParameter("type");
    switch (type) {
      case "add":
        add(req, resp);
        break;
      case "update":
        update(req, resp);

      default:
    }
  }

  private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String ids = req.getParameter("ids");

    if (ids != null) {
      String[] arr = ids.split(",");
      int[] ints = new int[arr.length];
      for (int i = 0; i < arr.length; i++) {
        ints[i] = Integer.valueOf(arr[i]);
      }
      informDao.delete(ints);
    }
    List<Inform> informs = informDao.queryByPage(1, 3);
    req.setAttribute("informList", informs);
    req.setAttribute("currentPage",1);
    req.getRequestDispatcher("/notice/notice.jsp").forward(req, resp);
  }

  private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String id = req.getParameter("id");
    String title = req.getParameter("title");
    String content = req.getParameter("content");

    Inform inform = new Inform();
    inform.setId(Integer.valueOf(id));
    inform.setTitle(title);
    inform.setContent(content);

    informDao.update(inform);


    Inform informRes = informDao.queryById(Integer.valueOf(id));
    req.setAttribute("inform", informRes);

    req.getRequestDispatcher("/notice/showUpdateNotice.jsp?succ=true").forward(req, resp);
  }


  public void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    String tilte = req.getParameter("title");
    String content = req.getParameter("content");
    String creatorId = req.getParameter("creatorId");

    Inform inform = new Inform();
    inform.setTitle(tilte);
    inform.setContent(content);
    inform.setCreatorId(Integer.valueOf(creatorId));

    informDao.addInform(inform);

    resp.setCharacterEncoding("utf-8");
    ServletOutputStream outputStream = resp.getOutputStream();
    outputStream.write(
        "<script type='text/javascript'>alert('添加成功');document.location.href='/notice/showAddNotice.jsp';</script>"
            .getBytes("utf-8"));
    outputStream.flush();
    outputStream.close();
  }

  public void queryAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    List<Inform> informs = informDao.queryByPage(0, -1);

    req.setAttribute("informList", informs);
    req.setAttribute("currentPage", 1);
    req.getRequestDispatcher("/notice/notice.jsp").forward(req, resp);
  }

  public void showInform(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String id = req.getParameter("id");
    Inform inform = informDao.queryById(Integer.valueOf(id));
    req.setAttribute("inform", inform);
    req.getRequestDispatcher("/notice/showUpdateNotice.jsp").forward(req, resp);
  }
}
